<template>
  <div class="container">
    <Navbar title="收银台"></Navbar>
    <div class="goods">
      <div class="tit">商品信息</div>
      <div class="goods-item">
        <div class="goods-thumb">
          <img src="@/assets/images/default-vip.png" alt="">
        </div>
        <div class="goods-info">
          <div class="goods-name">{{goodsInfo.name}}</div>
          <div class="goods-label">{{goodsInfo.label}}</div>
          <div class="goods-charge">{{'￥' + goodsInfo.charge}}</div>
        </div>
      </div>
    </div>
    <van-cell title="使用优惠码" is-link />
    <div class="credit">
      <div class="payment-item" v-for="(item,index) in payments" :key="index">
        <img src="@/assets/images/pay_icon.png" alt="" class="icon">
        <span>{{item.name}}</span>
        <img src="@/assets/images/right.png" alt="" class="pay">
      </div>
    </div>
    <van-submit-bar :price="+goodsInfo.charge*100" decimal-length label="总计：" text-align="left" button-color="#000">
      <template #button>
        <van-button color="#000" @click="onSubmit">立即支付</van-button>
      </template>
  </van-submit-bar>
  </div>
</template>

<script>
import Navbar from '@/components/common/Navbar.vue';
import { getPayments, createOrder } from '@/network/order';

export default {
  components: {
    Navbar,
  },
  data() {
    return {
      goodsInfo: {
        id: this.$route.query.goods_id,
        name: this.$route.query.goods_name,
        label: this.$route.query.goods_label,
        charge: this.$route.query.goods_charge,
      },
      payments: [],
      orderInfo: {},
    };
  },
  methods: {
    onSubmit() {
      createOrder({ role_id: this.goodsInfo.id }).then((res) => {
        this.orderInfo = {
          orderId: res.data.order_id,
          price: res.data.charge,
          payment: 'handPay',
          type: this.$route.query.goods_type,
          id: res.data.user_id,
        };
      });
      setTimeout(() => {
        this.$router.push({
          path: '/order/pay',
          query: this.orderInfo,
        });
      }, 800);
    },
  },
  created() {
    getPayments().then((res) => {
      this.payments = res.data;
    });
  },
};
</script>

<style lang="less" scoped>
.container {
  height: 100vh;
  background-color: #f6f6f6;
  .goods {
    margin: 12px 0;
    padding: 14px 14px 20px;
    background-color: #fff;
    .goods-item {
      display: flex;
      margin-top: 28px;
      .goods-thumb {
        width: 104px;
        margin-right: 12px;
      }
      .goods-info {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        font-size: 15.2px;
        .goods-name,
        .goods-charge {
          font-weight: 700;
        }
        .goods-label {
          font-size: 12.8px;
          color: #ff4d4f;
        }
        .goods-charge {
          color: #fd3968;
        }
      }
    }
  }
  .payment-item {
    position: relative;
    display: flex;
    align-items: center;
    margin-top: 12px;
    background-color: #fff;
    height: 80px;
    .icon {
      width: 32px;
      margin: 0 12px;
    }
    .pay {
      position: absolute;
      right: 16px;
      width: 24px;
    }
  }
}
</style>
